-- TransAttrOfEquip

local IconRender = require("app.widget.renders.IconRender")
local TransAttrOfEquipRender = import(".renders.TransAttrOfEquipRender")
local ListViewVertical = require("app.widget.listview.ListViewVertical")

local Item_Num = 4

local ClassRef = app.mvc.createPanelClass(...)

ClassRef._leftInitXPos = nil -- 左侧item起始X坐标
ClassRef._rightInitXPos = nil
ClassRef._posXDistance = nil -- item的宽

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewBagTransAttrNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._leftInitXPos = self._ui["Panel/Panel_new/data_1" ]:getPositionX()
	self._rightInitXPos = self._ui["Panel/Panel_old/data_1" ]:getPositionX()
	self._posXDistance = self._ui["Panel/Panel_new/data_1" ]:getChildByName( "Panel" ):getContentSize().width

	-- 上方资源信息
	local costIconPath = GD:getResIcon( 92 )
	self._ui.Panel_money:getChildByName( "icon" ):loadTexture( costIconPath )
	self._ui.Panel_money:getChildByName( "num" ):setString( UD:getMagicDust() )
	self._ui.Panel_money:setTouchEnabled( true )
	self._ui.Panel_money:addTouchEventListener(function(sender,eventType)
		if eventType == ccui.TouchEventType.ended then
			local param = {itemId = 92,tipsType = Enums.TipsType.daibi}
			local boundingBoxTemp = sender:getBoundingBox()
			local worldPosTemp = sender:getParent():convertToWorldSpace( cc.p( boundingBoxTemp.x, boundingBoxTemp.y ) )
			boundingBoxTemp.x = worldPosTemp.x
			boundingBoxTemp.y = worldPosTemp.y
			param.boundingBox = boundingBoxTemp
			app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params = param})
		end
	end)

	-- 右侧装备
	for i = 1, Item_Num do
		local oldPanelNode = self._ui["Panel/Panel_old/data_" .. i ]
		display.setCascadeOpacityEnabled( oldPanelNode, true )

		local oldPanel = oldPanelNode:getChildByName( "Panel" )
		local oldPanelEmpty = oldPanel:getChildByName( "Panel_empty" )
		local oldPanelData = oldPanel:getChildByName( "Panel_data" )
		oldPanelEmpty:getChildByName( "choose" ):setVisible( false )
		oldPanelData:getChildByName( "disable" ):setVisible( false )
		oldPanelData:getChildByName( "choose" ):setVisible( false )
		oldPanelData:getChildByName( "Button_1" ):addClickEventListener(function() -- 转移按钮
			assert( self._leftSelectedIndex, " ------------------------>>>>>>>>>>>> 右侧操作的时候左侧选中槽位 = nil" )
			local rightOpAttrData = clone( self._rihgtValidAttr[ i ] )

			local correspondLeftAttrIndex = nil -- 该属性在左侧装备上存在同位置索引时 对应的UI位置索引
			for i,v in ipairs( self._leftAttrListSum ) do
				if v.posIdx == rightOpAttrData.posIdx then
					correspondLeftAttrIndex = i
					break
				end
			end

			local leftSelectedAttrData = self._leftAttrListSum[ self._leftSelectedIndex ] -- 左侧选中属性
			local beReplaceAttrFromTrans = leftSelectedAttrData.fromTrans -- 左侧选中属性是否转移而来
			local beReplaceAttrFromTransIndex = nil -- 转移而来的属性在转移属性缓存表中索引
			if beReplaceAttrFromTrans then
				for k,v in pairs( self._attrList ) do
					if v.posIdx == leftSelectedAttrData.posIdx then
						beReplaceAttrFromTransIndex = k
						break
					end
				end
			end

			-- 具体的属性替换逻辑
			local function attrChangeFunc()
				if beReplaceAttrFromTrans then
					self._attrList[ beReplaceAttrFromTransIndex ] = nil
				end
				rightOpAttrData.fromTrans = true
				table.insert( self._attrList, rightOpAttrData )
				self._leftAttrListSum[ self._leftSelectedIndex ] = rightOpAttrData

				self:playOpAction( self._leftSelectedIndex, i, true )
			end
			-- 低属性 替换 高属性处理函数
			local function lowAttrReplaceHighAttr()
				local confirmDialog = display.newConfirmDialog(L("tips_3000331"),
					function()
						attrChangeFunc()
					end)
				UIHandler:addPopDialog(confirmDialog)
			end

			if correspondLeftAttrIndex then -- 存在同类型属性？
				if correspondLeftAttrIndex ~= self._leftSelectedIndex then -- 存在同类型属性的时候，替换的却不是同类型的属性、这是规则不允许的
					display.pushToast( L("tips_3000330") )
				else -- 同类型属性之间发生转移
					if rightOpAttrData.quality >= leftSelectedAttrData.quality then -- 高品质替换同品质及以下？
						attrChangeFunc()
					else -- 低品质替换高品质？
						lowAttrReplaceHighAttr()
					end
				end
			else -- 不存在同类型属性。替换其他属性、或者空槽位
				if leftSelectedAttrData.propID then -- 非空槽
					if rightOpAttrData.quality >= leftSelectedAttrData.quality then -- 高品质替换同品质及以下？
						attrChangeFunc()
					else -- 低品质替换高品质？
						lowAttrReplaceHighAttr()
					end
				else
					attrChangeFunc()
				end
			end
		end)
	end

	-- 左侧装备
	for i = 1, Item_Num do
		local newPanelNode = self._ui["Panel/Panel_new/data_" .. i ]
		display.setCascadeOpacityEnabled( newPanelNode, true )

		local newPanel = newPanelNode:getChildByName( "Panel" )
		local newPanelEmpty = newPanel:getChildByName( "Panel_empty" )
		local newPanelData = newPanel:getChildByName( "Panel_data" )

		newPanelEmpty:getChildByName( "choose" ):setVisible( false )
		newPanelEmpty:addTouchEventListener(function(sender, eventType)
			if eventType == ccui.TouchEventType.ended then -- 选中空槽位
				for i2 = 1, Item_Num do
					local newPanelNodeTemp = self._ui["Panel/Panel_new/data_" .. i2 ]
					local newPanelTemp = newPanelNodeTemp:getChildByName( "Panel" )
					local newPanelEmptyTemp = newPanelTemp:getChildByName( "Panel_empty" )
					local newPanelDataTemp = newPanelTemp:getChildByName( "Panel_data" )

					newPanelDataTemp:getChildByName( "choose" ):setVisible( false )
					newPanelEmptyTemp:getChildByName( "choose" ):setVisible( false )
				end
				newPanelEmpty:getChildByName( "choose" ):setVisible( true )
				self._leftSelectedIndex = i
				if self._isAllAttrScroll and #self._attrList == 0 then -- 是万能属性条且还没有发生转移
					self:_updateRightEquipData()
					self:refreshLeftAndRightData()
					self:refreshAttrList()
				end
				self:refreshRightBtnStatus()
			end
		end)

		newPanelData:getChildByName( "disable" ):setVisible( false )
		newPanelData:getChildByName( "Button_1" ):setColor( display.COLOR_WHITE ) -- 禁止转移标记
		newPanelData:getChildByName( "Button_1" ):setVisible( false )
		newPanelData:getChildByName( "choose" ):setVisible( false )
		newPanelData:addTouchEventListener(function(sender, eventType)
			if eventType == ccui.TouchEventType.ended then -- 选中具体属性槽
				for i2 = 1, Item_Num do
					local newPanelNodeTemp = self._ui["Panel/Panel_new/data_" .. i2 ]
					local newPanelTemp = newPanelNodeTemp:getChildByName( "Panel" )
					local newPanelEmptyTemp = newPanelTemp:getChildByName( "Panel_empty" )
					local newPanelDataTemp = newPanelTemp:getChildByName( "Panel_data" )

					newPanelDataTemp:getChildByName( "choose" ):setVisible( false )
					newPanelEmptyTemp:getChildByName( "choose" ):setVisible( false )
				end
				newPanelData:getChildByName( "choose" ):setVisible( true )
				self._leftSelectedIndex = i
				if self._isAllAttrScroll and #self._attrList == 0 then
					self:_updateRightEquipData()
					self:refreshLeftAndRightData()
					self:refreshAttrList()
				end
				self:refreshRightBtnStatus()
			end
		end)

		-- 撤销按钮、说明有属性，且一定是转移过来的属性
		newPanelData:getChildByName( "Button_2" ):addClickEventListener(function()
			local leftOpAttrData = self._leftAttrListSum[ i ]
			assert( leftOpAttrData.fromTrans, " ---------------->>>>>>>>> 能点到撤销按钮，数据却不是转移而来? " )

			-- 重置回初始状态
			local temp = nil
			if i <= #self._leftValidAttr then
				temp = self._leftValidAttr[i]
			else
				temp = { fromTrans = false }
			end
			self._leftAttrListSum[ i ] = temp

			local indexOfTransAttrList = nil -- 转移而来的属性在转移属性缓存表中索引
			for k,v in pairs( self._attrList ) do
				if v.posIdx == leftOpAttrData.posIdx then
					indexOfTransAttrList = k
					break
				end
			end

			if indexOfTransAttrList ~= nil then
				self._attrList[ indexOfTransAttrList ] = nil
			end
			self._leftSelectedIndex = i
			self:autoUndoAttrAfterRevoke()
			self:refreshRightBtnStatus()
			local rightOpIndex = nil
			for i,v in ipairs( self._rihgtValidAttr ) do
				if v.posIdx == leftOpAttrData.posIdx  then
					rightOpIndex = i
					break
				end
			end
			assert( rightOpIndex, " -------------------------->>>>> 撤销操作没找到右侧对应的槽位索引" )

			self:playOpAction( i, rightOpIndex, false )
		end)
	end

	self._ui["Panel/Panel_bom/Button_enter"]:addClickEventListener(function()
		if #self._attrList > 0 then
			if self._rightEquipData:getItemType() == Enums.ItemType.transattr 
			or self._rightEquipData:getItemType() == Enums.ItemType.allAttrScroll then
				local oriAttrVal = nil
				local flag = false
				for i, v in ipairs(self._leftEquipData.extraProps) do
					if v.posIdx == self._attrList[1].posIdx then
						local propConf = GD:getPropConfig(v.propID)
						if propConf and propConf.format == "2" then
							oriAttrVal = v.val
							break
						end
					end
				end
				if oriAttrVal then
					local strList = string.split(GD:getSystemStrPar("extraPropScroll"), "|")
					for i, v in ipairs(strList) do
						if tonumber(v) == oriAttrVal then
							flag = true
							break
						end
					end
				end
				-- 属性条卷轴（目前只有一条转移属性）
				local data = {
					self._rightEquipData.tid,
					self._leftEquipData.uid,
					self._attrList[1].posIdx,
				}
				if flag then
					local createDialog = display.newConfirmDialog( string.format(L("TransAttrOfEquipTips2"), oriAttrVal/10000*100 .."%"),
                    function(sender) -- 确定
						executeFunc(self._onOperate, "AddEquipExtraProp_sure", data)
						executeFunc(self._onCloseView)
                    end)
					UIHandler:addPopDialog( createDialog )
					return
				else
					executeFunc(self._onOperate, "AddEquipExtraProp_sure", data)
				end				
			else
				local temp = {}
				for k,v in pairs( self._attrList ) do
					local srcIndexTemp = nil
					for i2,v2 in ipairs( self._rightEquipData.extraProps ) do
						if v.posIdx == v2.posIdx then
							srcIndexTemp = v2.posIdx
							break
						end
					end
					assert( srcIndexTemp, " -------------------->>>>>>>>>> 属性来源居然为nil " )

					local destIndexTemp = nil
					local indexof = table.indexof( self._leftAttrListSum, v )
					if indexof <= #self._leftValidAttr then -- 有原生属性
						local dataTemp = self._leftValidAttr[indexof]
						for i2,v2 in ipairs( self._leftEquipData.extraProps ) do
							if dataTemp.posIdx == v2.posIdx then
								destIndexTemp = v2.posIdx
								break
							end
						end
					else -- 空槽位
						local leftNullIdx = {} -- 左侧剩余的空槽索引
						for i = 1, self._leftAttrSum do
							leftNullIdx[i] = true -- { true, true, true,,, }
						end
						for i,v in ipairs( self._leftValidAttr ) do
							leftNullIdx[ v.posIdx ] = false -- 假设{ false, true, true }
						end

						local UIidx = indexof - #self._leftValidAttr -- UI上的槽位索引
						local nullCount = 0
						for i,v in ipairs( leftNullIdx ) do
							if v then
								nullCount = nullCount + 1
							end
							if UIidx == nullCount then
								destIndexTemp = i
								break
							end
						end
					end
					assert( destIndexTemp, " -------------------->>>>>>>>>> 属性替换的目标居然为nil " )

					table.insert( temp, srcIndexTemp )
				end

				local data = {
					self._rightEquipData.uid,
					self._leftEquipData.uid,
					temp
				}
				executeFunc(self._onOperate, "TransAttrOfEquip_sure", data)
			end
		end

		-- 如果目标装备(左边)没绑定的话要二级提示
		executeFunc(self._onCloseView)
	end)

	self._ui["Panel/Button_close"]:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)

	self._costNeededBg = self._ui["Panel/Panel_bom/bg"] -- 转移消耗资源底图
	self._costNeededIcon = self._ui["Panel/Panel_bom/icon"] -- 转移消耗资源图标
	self._costNeededStr = self._ui["Panel/Panel_bom/BitmapFontLabel_1"] -- 转移消耗资源数量

	-- 说明操作
	self._ui["Panel/Panel_bom/Button_explain"]:addClickEventListener(function()
		-- executeFunc(self._onPanelHandler, "TransAttrOfEquip_explain")
	end)

	-- 左侧装备列表
	local params = {
		renderClass = TransAttrOfEquipRender,
		perNum = 1,
		paddingLeft = 15,
		cellEventProxy = function(eventName, cellModel)
			if eventName == "click" then
				self:onChangeSelectedEquip(cellModel:getIndex())
			end
		end,
	}
	self._listview = ListViewVertical.new(self._ui["Panel/Panel_list/ScrollView"], params)
	self._ui.Panel_list:setVisible(false)

	self._attrList = {} -- 转移的属性暂存列表
	self._leftAttrListSum = {} -- 左侧装备的属性列表、在发生过操作之后的
	self._leftSelectedIndex = nil -- 左侧选中的槽位索引
	self._selectedEquipIndex = 1 -- 选中的要转移的装备索引，默认选中第一个
	self._isAllAttrScroll = false -- 是否是用万能属性条转移属性
	self._leftPropIdList = {} -- 使用万能属性条转移时，左侧装备按照UI上索引对应的属性列表
end

-- 转移的装备 uid
function ClassRef:refresh() -- self._data = equip.uid
	if self._data then

		self._rightEquipData = UD:getItem( self._data )
		self._leftEquipData = UD:getEquipByPos( self._rightEquipData:getPos() )

		assert( self._rightEquipData, " -------------->>>>>>>>>>>>>> 右侧装备无数据？ " )
		assert( self._leftEquipData, " -------------->>>>>>>>>>>>>> 左侧装备无数据？ " )

		self:refreshLeftAndRightData()
		self:adjustSelectedIndex(1)
		self:refreshAttrList()
	else
		self:setVisible( false )
	end
end

function ClassRef:refreshLeftAndRightData()
	-------------------------------------->>>>>>>>>>>>>>>>>>>>>> refresh right equip
	local rightEquipIcon = IconRender.createWithData( self._rightEquipData, self._ui["Panel/Panel_old/equit"]:getChildByName( "Panel" ):getChildByName( "icon" ) )
	rightEquipIcon:setClickEnable( false )
	rightEquipIcon:setEquiped( false )
	self._ui["Panel/Panel_old/equit"]:getChildByName( "Panel" ):getChildByName( "name" ):setString( self._rightEquipData:getName() )
	self._ui["Panel/Panel_old/equit"]:getChildByName( "Panel" ):getChildByName( "name" ):setTextColor( Enums.QualityColor[self._rightEquipData.quality] )

	-- local strTemp = self._ui["Panel/Panel_old/equit"]:getChildByName( "Panel" ):getChildByName( "Text" ):getString()
	-- strTemp = string.gsub( strTemp, "%d", "")

	-- 右侧提供的有效属性
	self._rihgtValidAttr = {}
	for i,v in ipairs( self._rightEquipData.extraProps ) do
		local extraAttrEx = self._rightEquipData:ctorExtraProp( v )
		extraAttrEx.fromTrans = false
		table.insert( self._rihgtValidAttr, extraAttrEx )
	end
	self._rightAttrSum = self._rightEquipData:getExtraAttrNum() -- 右侧属性槽位总数
	
	self._ui["Panel/Panel_old/equit"]:getChildByName( "Panel" ):getChildByName( "Text" ):setString( #self._rihgtValidAttr .. L("equipaz_tips1") )

	-------------------------------------->>>>>>>>>>>>>>>>>>>>>> refresh left equip
	local leftEquipIcon = IconRender.createWithData( self._leftEquipData, self._ui["Panel/Panel_new/equit"]:getChildByName( "Panel" ):getChildByName( "icon" ) )
	leftEquipIcon:setClickEnable( false )
	leftEquipIcon:setEquiped( false )
	self._ui["Panel/Panel_new/equit"]:getChildByName( "Panel" ):getChildByName( "name" ):setString( self._leftEquipData:getName() )
	self._ui["Panel/Panel_new/equit"]:getChildByName( "Panel" ):getChildByName( "name" ):setTextColor( Enums.QualityColor[self._leftEquipData.quality] )

	-- local strTemp = self._ui["Panel/Panel_new/equit"]:getChildByName( "Panel" ):getChildByName( "Text" ):getString()
	-- strTemp = string.gsub( strTemp, "%d", "")

	-- 左侧装备自带属性
	self._leftValidAttr = {}
	
	for i,v in ipairs( self._leftEquipData.extraProps ) do
		local extraAttrEx = self._leftEquipData:ctorExtraProp( v )
		extraAttrEx.fromTrans = false
		table.insert( self._leftValidAttr, extraAttrEx )
	end
	self._leftAttrSum = self._leftEquipData:getExtraAttrNum() -- 左侧属性槽位总数
	self._ui["Panel/Panel_new/equit"]:getChildByName( "Panel" ):getChildByName( "Text" ):setString( #self._leftValidAttr .. L("equipaz_tips1") )

	-- leftSumAttrList - 左侧装备总属性列表、包含转移操作之后的属性
	self._leftAttrListSum = {}
	for i,v in ipairs( self._leftValidAttr ) do
		table.insert( self._leftAttrListSum, v )
	end
	for i = #self._leftValidAttr + 1, self._leftAttrSum do
		self._leftAttrListSum[i] = { fromTrans = false }
	end
end

-- 万能属性条转移
function ClassRef:refreshTransAttrView(data)
	self._isAllAttrScroll = true
	if data then
		self._data = data
		self._ui.Panel_list:setVisible(true)
		self._ui["Panel/Panel_bom/tips"]:setString(L("TransAttrOfEquipTips1"))
		local equips = UD:getCanTransAttrEquips()
		self._listview:reloadData(equips)
		self:_updateRightPanel()
	else
		self:setVisible(false)
	end
end

function ClassRef:onChangeSelectedEquip(index)
	if index == self._selectedEquipIndex then
		return
	end
	self._attrList = {} -- 切换装备时需要清理转移的属性暂存列表
	self._leftSelectedIndex = nil
	self._selectedEquipIndex = index
	self._listview:setFoucsIndex(index)
	self:_updateRightPanel()
end

function ClassRef:_updateRightPanel()
	if self._data then		
		self._leftEquipData = self._listview:getDataByIndex(self._selectedEquipIndex)
		self._rightEquipData = UD:getItem(self._data)

		-- 左侧装备每个槽位对应的属性
		self._leftPropIdList = {}
		for i, v in ipairs(self._leftEquipData.extraProps) do
			local propId = self._leftEquipData:getExtraAttrIdByPos(v.posIdx)
			self._leftPropIdList[v.posIdx] = {propID = propId, posIdx = v.posIdx, isHave = true}
		end
		for i = 1, self._leftEquipData:getExtraAttrNum() do
			if not self._leftPropIdList[i] then
				local propId = self._leftEquipData:getExtraAttrIdByPos(i)
				self._leftPropIdList[i] = {propID = propId, posIdx = i, isHave = false}
			end
		end
		table.sort(self._leftPropIdList, function(a, b)
			if a.isHave ~= b.isHave then
				return a.isHave
			else
				return a.posIdx < b.posIdx
			end
		end)

		self:_updateRightEquipData(1) -- 默认先选中第一个槽位
		self:refreshLeftAndRightData()
		self:adjustSelectedIndex(1)
		self:refreshAttrList()
	end
end

-- 根据选中的槽位改变万能属性条的附加属性
function ClassRef:_updateRightEquipData(index)
	index = index or self._leftSelectedIndex
	local info = self._leftPropIdList[index]
	if info then
		self._rightEquipData:setExtraProps(info.propID, info.posIdx)
	end
end

-- 刷新装备属性列表
function ClassRef:refreshAttrList()
	-------------------------------------->>>>>>>>>>>>>>>>>>>>>> refresh right equip attr list
	for i = 1, Item_Num do
		local dataOfIndex, hadTransed = self:getRightAttrDataByIndex( i )
		local renderTemp = self._ui["Panel/Panel_old/data_" .. i ]
		local renderPanel = renderTemp:getChildByName("Panel")
		local renderPanelData = renderPanel:getChildByName("Panel_data")
		local renderPanelEmpty = renderPanel:getChildByName("Panel_empty")
		if dataOfIndex then -- 有数据？
			renderTemp:setVisible( true )

			if hadTransed then -- 发生过转移？
				renderPanelData:setVisible( false )

				renderPanelEmpty:setVisible( true )
				renderPanelEmpty:getChildByName("choose"):setVisible( false )
			else
				renderPanelEmpty:setVisible( false )

				renderPanelData:setVisible( true )
				renderPanelData:getChildByName("Button_1"):setVisible( true )
				renderPanelData:getChildByName("choose"):setVisible( false )
				renderPanelData:getChildByName("Button_2"):setVisible( false )

				local _, _, attrText = UD:formatAttr( dataOfIndex.propID, dataOfIndex.val )
				renderPanelData:getChildByName("Text"):setString( attrText )
				renderPanelData:getChildByName("Text"):setTextColor( Enums.QualityColor[ dataOfIndex.quality ] )
			end
		else
			if i > self._rightAttrSum then
				renderTemp:setVisible( false )
			else
				renderTemp:setVisible( true )
				renderPanelData:setVisible( false )
				renderPanelEmpty:setVisible( true )
				renderPanelEmpty:getChildByName("choose"):setVisible( false )
			end
		end
	end

	-------------------------------------->>>>>>>>>>>>>>>>>>>>>> refresh left equip attr list
	for i = 1, Item_Num do
		local dataOfIndex, fromTransFlag = self:getLeftAttrDataByIndex( i )
		local renderTemp = self._ui["Panel/Panel_new/data_" .. i ]
		local renderPanel = renderTemp:getChildByName("Panel")
		local renderPanelData = renderPanel:getChildByName("Panel_data")
		local renderPanelEmpty = renderPanel:getChildByName("Panel_empty")
		if dataOfIndex and dataOfIndex.propID then -- 有数据？
			renderTemp:setVisible( true )

			renderPanelEmpty:setVisible( false )
			renderPanelData:setVisible( true )
			if i == self._leftSelectedIndex then -- 选中的是当前槽位？
				renderPanelData:getChildByName("choose"):setVisible( true )
			else
				renderPanelData:getChildByName("choose"):setVisible( false )
			end
			if fromTransFlag then -- 转移而来？
				renderPanelData:getChildByName("Button_2"):setVisible( true )
			else
				renderPanelData:getChildByName("Button_2"):setVisible( false )
			end

			local _, _, attrText = UD:formatAttr( dataOfIndex.propID, dataOfIndex.val )
			renderPanelData:getChildByName("Text"):setString( attrText )
			renderPanelData:getChildByName("Text"):setTextColor( Enums.QualityColor[ dataOfIndex.quality ] )
		else
			if i > self._leftAttrSum then
				renderTemp:setVisible( false )
			else
				renderTemp:setVisible( true )
				renderPanelData:setVisible( false )
				renderPanelEmpty:setVisible( true )
				if i == self._leftSelectedIndex then -- 选中的是当前槽位？
					renderPanelEmpty:getChildByName("choose"):setVisible( true )
				else
					renderPanelEmpty:getChildByName("choose"):setVisible( false )
				end
			end
		end
	end

	-- 刷新消耗
	local costIconPath = GD:getResIcon( 92 )
	self._costNeededIcon:loadTexture( costIconPath )
	local costSum = 0
	
	for i,v in pairs( self._attrList ) do
		local conf = GD:querypropExtraByPropIDandQuality( v.propID, v.quality )
		costSum = costSum + conf.costPar * tonumber(GD:getSystemPar("extraPropCostBase",1000))
	end
	self._costNeededStr:setString( UD:getMagicDust() .. "/" .. costSum )
	local flag = self._rightEquipData:getItemType() ~= Enums.ItemType.transattr --属性条卷轴不消耗魔尘
	local flag2 = self._rightEquipData:getItemType() ~= Enums.ItemType.allAttrScroll -- 万能属性条卷轴也不消耗魔尘
	self._costNeededBg:setVisible(flag and flag2)
	self._costNeededIcon:setVisible(flag and flag2)
	self._costNeededStr:setVisible(flag and flag2)

	-- 刷新按钮状态
	if #self._attrList > 0 then
		self._ui["Panel/Panel_bom/Button_enter"]:setTouchEnabled( true )
		self._ui["Panel/Panel_bom/Button_enter"]:setColor( display.COLOR_WHITE )
	else
		self._ui["Panel/Panel_bom/Button_enter"]:setTouchEnabled( false )
		self._ui["Panel/Panel_bom/Button_enter"]:setColor( display.COLOR_GRAY )
	end
end

-- 左侧选中槽位时，刷新右侧按钮的操作状态
function ClassRef:refreshRightBtnStatus()
	for i = 1, Item_Num do
		local dataOfIndex, hadTransed = self:getRightAttrDataByIndex( i )
		local renderTemp = self._ui["Panel/Panel_old/data_" .. i ]
		local renderPanel = renderTemp:getChildByName("Panel")
		local renderPanelData = renderPanel:getChildByName("Panel_data")
		local renderPanelDataBtn_1 = renderPanelData:getChildByName("Button_1")
		local renderPanelDataText = renderPanelData:getChildByName("Text")
		local renderPanelDataDisable = renderPanelData:getChildByName("disable")

		if dataOfIndex then -- 有数据？
			if hadTransed then -- 发生过转移？
				renderPanelDataBtn_1:setBright( true )
				renderPanelDataBtn_1:setTouchEnabled( true )
				renderPanelDataText:setTextColor( Enums.QualityColor[ dataOfIndex.quality ] )
				renderPanelDataDisable:setVisible( false )
			else
				local rightOpAttrData = clone( self._rihgtValidAttr[ i ] )
				local correspondLeftAttrIndex = nil -- 该属性在左侧装备上存在同位置属性时候对应的位置索引
				for i,v in ipairs( self._leftAttrListSum ) do
					if v.posIdx == rightOpAttrData.posIdx then
						correspondLeftAttrIndex = i
						break
					end
				end
				if correspondLeftAttrIndex then -- 存在同位置属性？
					if correspondLeftAttrIndex ~= self._leftSelectedIndex then -- 存在同位置属性的时候，替换的却不是同位置的属性、这是规则不允许的
						renderPanelDataBtn_1:setBright( false )
						renderPanelDataBtn_1:setTouchEnabled( false )
						-- renderPanelDataText:setTextColor( cc.c3b( 116, 116, 116 ) )
						renderPanelDataDisable:setVisible( true )
					else
						renderPanelDataBtn_1:setBright( true )
						renderPanelDataBtn_1:setTouchEnabled( true )
						renderPanelDataText:setTextColor( Enums.QualityColor[ dataOfIndex.quality ] )
						renderPanelDataDisable:setVisible( false )
					end
				else
					if self._leftAttrListSum[ self._leftSelectedIndex ].posIdx then -- 但是左侧选中栏存在属性？
						-- 说明此属性跟当前右侧操作属性posIdx不相同
						renderPanelDataBtn_1:setBright( false )
						renderPanelDataBtn_1:setTouchEnabled( false )
						-- renderPanelDataText:setTextColor( cc.c3b( 116, 116, 116 ) )
						renderPanelDataDisable:setVisible( true )
					else -- 空槽？
						renderPanelDataBtn_1:setBright( true )
						renderPanelDataBtn_1:setTouchEnabled( true )
						renderPanelDataText:setTextColor( Enums.QualityColor[ dataOfIndex.quality ] )
						renderPanelDataDisable:setVisible( false )
					end
				end
			end
		else
			renderPanelDataBtn_1:setBright( true )
			renderPanelDataBtn_1:setTouchEnabled( true )
			renderPanelDataText:setTextColor( display.COLOR_WHITE )
			renderPanelDataDisable:setVisible( false )
		end
	end
end

-- 调整发生属性转移之后的选中槽位
function ClassRef:adjustSelectedIndex(defaultSelect)
	-- 如果左侧装备还有空槽位，自动选中空槽位。否则，停留在最后一次操作的槽位上
	for i,v in ipairs( self._leftAttrListSum ) do
		if not v.propID then -- 空槽位
			self._leftSelectedIndex = i
			self:refreshRightBtnStatus()
			return
		else
			if not v.fromTrans then
				for _, rv in pairs(self._rihgtValidAttr) do
					if rv.propID == v.propID then
						if rv.val > v.val then
							self._leftSelectedIndex = i
							self:refreshRightBtnStatus()
							return
						end
						break
					end
				end
			end
		end
	end
	if defaultSelect then
		self._leftSelectedIndex = defaultSelect
		self:refreshRightBtnStatus()
	end
end

-- 自动撤销所有转移过来的冲突属性
-- 在玩家手动撤销某一条转移过来的属性之后 导致的多条同类型属性冲突时候的解决办法
function ClassRef:autoUndoAttrAfterRevoke()
	local firstSameAttrIndex = nil
	local secondSameAttrIndex = nil
	for i = 1, #self._leftAttrListSum do
		for j = i + 1, #self._leftAttrListSum do
			if self._leftAttrListSum[i].posIdx and self._leftAttrListSum[j].posIdx and self._leftAttrListSum[i].posIdx == self._leftAttrListSum[j].posIdx then
				firstSameAttrIndex = i
				secondSameAttrIndex = j
				break
			end
		end
	end
	if firstSameAttrIndex and secondSameAttrIndex then -- 存在重复的同类型属性
		local firstSameAttrTemp = self._leftAttrListSum[firstSameAttrIndex]
		local secondSameAttrTemp = self._leftAttrListSum[secondSameAttrIndex]

		-- 有可能 firstSameAttrTemp == secondSameAttrTemp 即两条完全相同的属性发生替换

		-- 同类型属性之中必然只有一条转移而来
		if firstSameAttrTemp.fromTrans then
			assert( not secondSameAttrTemp.fromTrans, " ----------------->>>>>>>>>>>>>> 第一条属性是转移而来的时候，第二条也是转移而来？这不可能，某个位置状态量没设置对" )

			local firstSameAttrFromTransIndex = nil
			for k,v in pairs( self._attrList ) do
				if v.posIdx == firstSameAttrTemp.posIdx then -- 同位置属性
					firstSameAttrFromTransIndex = k
					break
				end
			end
			assert( firstSameAttrFromTransIndex, " ----------------->>>>>>>>>>>>>> 第一条属性是转移而来，却找不到它在转移属性表中的索引 " )

			local temp = nil
			if firstSameAttrIndex <= #self._leftValidAttr then
				temp = self._leftValidAttr[ firstSameAttrIndex ]
			else
				temp = { fromTrans = false }
			end
			self._leftAttrListSum[ firstSameAttrIndex ] = temp
			self._attrList[ firstSameAttrFromTransIndex ] = nil -- 从转移表中剔除

			-- 每次撤销之后再检查、直到不存在冲突的同类型属性
			self:autoUndoAttrAfterRevoke()
		else
			assert( secondSameAttrTemp.fromTrans, " ----------------->>>>>>>>>>>>>> 第一条属性不是转移而来的时候，第二条也不是转移而来？这也不可能，某个位置状态量没设置对" )

			local secondSameAttrFromTransIndex = nil
			for k,v in pairs( self._attrList ) do
				if v.posIdx == secondSameAttrTemp.posIdx then -- 同属性
					secondSameAttrFromTransIndex = k
					break
				end
			end
			assert( secondSameAttrFromTransIndex, " ----------------->>>>>>>>>>>>>> 第二条属性是转移而来，却找不到它在转移属性表中的索引 " )

			local temp = nil
			if secondSameAttrIndex <= #self._leftValidAttr then
				temp = self._leftValidAttr[ secondSameAttrIndex ]
			else
				temp = { fromTrans = false }
			end
			self._leftAttrListSum[ secondSameAttrIndex ] = temp
			self._attrList[ secondSameAttrFromTransIndex ] = nil -- 从转移表中剔除

			self:autoUndoAttrAfterRevoke()
		end
	end
end

-- 通过index索引找到左侧属性槽对应的数据
function ClassRef:getLeftAttrDataByIndex( index )
	if index > self._leftAttrSum then
		return nil
	else
		local dataTemp = self._leftAttrListSum[index]
		return dataTemp, dataTemp.fromTrans
	end
end



-- 通过index索引找到右侧属性槽对应的数据
function ClassRef:getRightAttrDataByIndex( index )
	if index > self._rightAttrSum then -- 数据索引越界
		return nil
	else
		local hadTransed = false -- 发生转移了？
		local dataTemp = self._rihgtValidAttr[index]
		if dataTemp then
			for k,v in pairs( self._attrList ) do
				if v.posIdx == dataTemp.posIdx then
					hadTransed = true
					break
				end
			end
		end

		return dataTemp, hadTransed
	end
end

-- 属性转移动画
function ClassRef:playOpAction( leftIndex, rightIndex, fromRightToLeft )
	assert( leftIndex and rightIndex, "------------------>>>>>>>>>> 属性转移动画，却没有索引" )

	local leftItem = self._ui["Panel/Panel_new" ]:getChildByName("data_"..leftIndex)
	local leftItemPosY = leftItem:getPositionY()

	local rightItem = self._ui["Panel/Panel_old" ]:getChildByName("data_"..rightIndex)
	local rightItemPosY = rightItem:getPositionY()

	if fromRightToLeft then -- 属性从右 -->> 左转移

		-------------------------------->>>>>>>>>>>>>>>>>>>>> 左侧item动画
		local leftAction1 = cc.ScaleTo:create( 0.2, 0 )
		local leftAction2 = cc.FadeTo:create( 0.2, 0 )
		local leftAction3 = cc.MoveTo:create( 0.2, cc.p( ( self._leftInitXPos - self._posXDistance ) , leftItemPosY ) )
		local leftFlyOutAction = cc.Spawn:create( leftAction1, leftAction2, leftAction3 )

		local leftAdjustPosAction = cc.CallFunc:create(function()
			leftItem:setPositionX( self._leftInitXPos + self._posXDistance )

			self:adjustSelectedIndex()
			self:refreshAttrList()
		end)

		local leftAction4 = cc.ScaleTo:create( 0.2, 1 )
		local leftAction5 = cc.FadeTo:create( 0.2, 255 )
		local leftAction6 = cc.MoveTo:create( 0.2, cc.p( self._leftInitXPos , leftItemPosY ) )
		local leftFlyInAction = cc.Spawn:create( leftAction4, leftAction5, leftAction6 )

		local leftAction = cc.Sequence:create( leftFlyOutAction, leftAdjustPosAction, leftFlyInAction )
		leftItem:runAction( leftAction )


		-------------------------------->>>>>>>>>>>>>>>>>>>>> 右侧item动画
		local rightAction1 = cc.ScaleTo:create( 0.4, 0 )
		local rightAction2 = cc.FadeTo:create( 0.4, 0 )
		local rightAction3 = cc.MoveTo:create( 0.4, cc.p( ( self._rightInitXPos - self._posXDistance ) , rightItemPosY ) )
		local rightFlyOutAction = cc.Spawn:create( rightAction1, rightAction2, rightAction3 )

		local rightAdjustPosAction = cc.CallFunc:create(function()
			rightItem:setPositionX( self._rightInitXPos )
			rightItem:setScale( 1 )
			rightItem:setOpacity( 255 )
		end)

		local rightAction = cc.Sequence:create( rightFlyOutAction, rightAdjustPosAction )
		rightItem:runAction( rightAction )
	else -- 从左 -->> 右

		-------------------------------->>>>>>>>>>>>>>>>>>>>> 左侧item动画
		local leftAction1 = cc.ScaleTo:create( 0.4, 0 )
		local leftAction2 = cc.FadeTo:create( 0.4, 0 )
		local leftAction3 = cc.MoveTo:create( 0.4, cc.p( ( self._leftInitXPos + self._posXDistance ) , leftItemPosY ) )
		local leftFlyOutAction = cc.Spawn:create( leftAction1, leftAction2, leftAction3 )

		local leftAdjustPosAction = cc.CallFunc:create(function()
			leftItem:setPositionX( self._leftInitXPos )
			leftItem:setScale( 1 )
			leftItem:setOpacity( 255 )
		end)

		local leftAction = cc.Sequence:create( leftFlyOutAction, leftAdjustPosAction )
		leftItem:runAction( leftAction )

		-------------------------------->>>>>>>>>>>>>>>>>>>>> 右侧item动画
		local rightAction1 = cc.ScaleTo:create( 0.2, 0 )
		local rightAction2 = cc.FadeTo:create( 0.2, 0 )
		local rightAction3 = cc.MoveTo:create( 0.2, cc.p( ( self._rightInitXPos + self._posXDistance ) , rightItemPosY ) )
		local rightFlyOutAction = cc.Spawn:create( rightAction1, rightAction2, rightAction3 )

		local rightAdjustPosAction = cc.CallFunc:create(function()
			rightItem:setPositionX( self._rightInitXPos - self._posXDistance )

			self:refreshAttrList()
		end)

		local rightAction4 = cc.ScaleTo:create( 0.2, 1 )
		local rightAction5 = cc.FadeTo:create( 0.2, 255 )
		local rightAction6 = cc.MoveTo:create( 0.2, cc.p( self._rightInitXPos , rightItemPosY ) )
		local rightFlyInAction = cc.Spawn:create( rightAction4, rightAction5, rightAction6 )

		local rightAction = cc.Sequence:create( rightFlyOutAction, rightAdjustPosAction, rightFlyInAction )
		rightItem:runAction( rightAction )
	end
end


function ClassRef:onCloseView( cb )
	self._onCloseView = function()
		self._leftValidAttr = {}
		self._rihgtValidAttr = {}
		self._attrList = {} -- 转移的属性暂存列表
		self._leftAttrListSum = {} -- 左侧装备的属性列表、在发生过操作之后的
		self._leftSelectedIndex = nil -- 左侧选中的槽位索引
		self._selectedEquipIndex = 1 -- 选中的要转移的装备索引，默认选中第一个
		self._isAllAttrScroll = false -- 是否是用万能属性条转移属性

		executeFunc(cb)
	end
end

function ClassRef:onOperate( cb )
	self._onOperate = cb
end

return ClassRef
